﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace badminton.DynamicData.CustomPages
{
    public partial class CourtSchedule : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {


            if (!IsPostBack)
            {
                int courtid = -1;
                if (this.Request.QueryString["CourtId"] != null)
                    courtid = Convert.ToInt32(this.Request.QueryString["CourtId"]);
                BadmintonEntities bde = new BadmintonEntities();
                Court court = bde.Court.FirstOrDefault(p => p.id == courtid);
                if (court == null)
                {
                    Hall hh = bde.Hall.FirstOrDefault();
                    if (hh!=null)
                    {
                        court = hh.Court.FirstOrDefault();
                    }
                }

                if (court==null)
                {
                    return;
                }

                foreach (Hall hall in bde.Hall)
                {
                    DropDownList1.Items.Add(new ListItem(hall.name, hall.id.ToString()));
                }
                DropDownList1.SelectedValue = court.hallId.Value.ToString();

                Hall theHall = court.Hall;
                if (theHall == null)
                {
                    return;
                }
                foreach (Court ct in theHall.Court)
                {
                    DropDownList2.Items.Add(new ListItem(ct.name, ct.id.ToString()));
                }
                DropDownList2.SelectedValue = court.id.ToString();


                this.LabelEditingId.Text = "-1";
                this.TextStartTime.Text = DateTime.Now.ToString("yyyy.MM.dd");
                this.TextEndTime.Text = DateTime.Now.ToString("yyyy.MM.dd");
                foreach(CourtScheduleType cst in bde.CourtScheduleType)
                {
                    this.DropDownListUserType.Items.Add(new ListItem(cst.name, cst.id.ToString()));
                }

                for (int i = 0; i < 24; i++)
                {
                    DropDownListStartTimeHour.Items.Add(new ListItem(i.ToString(), i.ToString()));
                    DropDownListEndTimeHour.Items.Add(new ListItem(i.ToString(), i.ToString()));
                }

                for (int i = 0; i < 60; i++)
                {
                    DropDownListStartTimeMinute.Items.Add(new ListItem(i.ToString(), i.ToString()));
                    DropDownListEndTimeMinute.Items.Add(new ListItem(i.ToString(), i.ToString()));
                }


                bindGridview();
            }
        }

        protected void bindGridview()
        {
            if (DropDownList2.Items.Count==0)
            {
                return;
            }

            int courtid = Convert.ToInt32(this.DropDownList2.SelectedValue);
            BadmintonEntities bde = new BadmintonEntities();
            Court court = bde.Court.FirstOrDefault(p => p.id == courtid);
            if (court == null)
            {
                return;
            }
            GridView1.DataSource = court.CourtSchedule;
            GridView1.DataBind();

        }

        public string GetFiledValue(object id, object type)
        {
            int csid = (int)id;
            string strtype = type.ToString();
            BadmintonEntities bde = new BadmintonEntities();
            badminton.CourtSchedule cs = bde.CourtSchedule.FirstOrDefault(p => p.id == csid);
            if (cs==null)
            {
                return "";
            }

            if (strtype == "使用类型")
            {
                if (cs.CourtScheduleType !=null)
                {
                    return cs.CourtScheduleType.name;
                }
            }
            else if (strtype == "备注")
            {
                return cs.remark;
            }
            else if (strtype == "球场")
            {
                return "~/Court/Details.aspx?CourtId=" + cs.courtId.ToString();
            }

            return "";
        }


        protected void OnRowCommand(object sender, CommandEventArgs e)
        {
            if (e.CommandName == "New")
            {
                this.LabelEditingId.Text = "-1";
                this.LinkButtonEditOrNew.Text = "插入";
            }
            else if (e.CommandName == "EditRow")
            {
                int csid = Convert.ToInt32(e.CommandArgument as string);
                BadmintonEntities bde = new BadmintonEntities();
                badminton.CourtSchedule cs = bde.CourtSchedule.FirstOrDefault(p => p.id == csid);
                if (cs!=null)
                {
                    this.LabelEditingId.Text = cs.id.ToString();
                    if (cs.startTime != null)
                    {
                        this.TextStartTime.Text = cs.startTime.Value.ToString("yyyy.MM.dd");
                        this.DropDownListStartTimeHour.SelectedValue = cs.startTime.Value.Hour.ToString();
                        this.DropDownListStartTimeMinute.SelectedValue = cs.startTime.Value.Minute.ToString();
                    }
                    else
                    {
                        this.TextStartTime.Text = DateTime.Now.ToString("yyyy.MM.dd");
                    }
                    if (cs.endTime != null)
                    {
                        this.TextEndTime.Text = cs.endTime.Value.ToString("yyyy.MM.dd");
                        this.DropDownListEndTimeHour.SelectedValue = cs.endTime.Value.Hour.ToString();
                        this.DropDownListEndTimeMinute.SelectedValue = cs.endTime.Value.Minute.ToString();
                    }
                    else
                    {
                        this.TextEndTime.Text = DateTime.Now.ToString("yyyy.MM.dd");
                    }

                    this.LinkButtonEditOrNew.Text = "编辑";
                }
            }
            else if (e.CommandName == "DeleteRow")
            {
                int csid = Convert.ToInt32(e.CommandArgument as string);
                BadmintonEntities bde = new BadmintonEntities();
                badminton.CourtSchedule cs = bde.CourtSchedule.FirstOrDefault(p => p.id == csid);
                if (cs != null)
                {
                    bde.DeleteObject(cs);
                    bde.SaveChanges();
                    bindGridview();
                }
            }

        }

        protected void OnCreatSchedule(object sender, CommandEventArgs e)
        {
            if (e.CommandName=="取消")
            {
                this.LabelEditingId.Text = "-1";
                this.LinkButtonEditOrNew.Text = "插入";

                this.TextStartTime.Text = DateTime.Now.ToString("yyyy.MM.dd");
                this.TextEndTime.Text = DateTime.Now.ToString("yyyy.MM.dd");
                this.TextBox1.Text = "";
            }
            else
            {
                DateTime starttime = DateTime.Parse(this.TextStartTime.Text);
                starttime = starttime.AddHours(Convert.ToInt32(this.DropDownListStartTimeHour.SelectedValue));
                starttime = starttime.AddMinutes(Convert.ToInt32(this.DropDownListStartTimeMinute.SelectedValue));

                DateTime endtime = DateTime.Parse(this.TextEndTime.Text);
                endtime = endtime.AddHours(Convert.ToInt32(this.DropDownListEndTimeHour.SelectedValue));
                endtime = endtime.AddMinutes(Convert.ToInt32(this.DropDownListEndTimeMinute.SelectedValue));
                if (starttime.Date != endtime.Date)
                {
                    Response.Write("<script type='text/javascript'>alert('开始和结束时间必须在同一天！');</script>");
                    return;
                }

                if (starttime >= endtime)
                {
                    Response.Write("<script type='text/javascript'>alert('开始时间应早于结束时间！');</script>");
                    return;
                }


                BadmintonEntities bde = new BadmintonEntities();
                int courtid = Convert.ToInt32(this.DropDownList2.SelectedValue);
                Court ct = bde.Court.FirstOrDefault(p => p.id == courtid);
                if (ct == null)
                {
                    return;
                }

                foreach (badminton.CourtSchedule cs in ct.CourtSchedule.OrderBy(p => p.startTime))
                {
                    if (this.LabelEditingId.Text != "-1" && this.LabelEditingId.Text == cs.id.ToString())
                    {
                        continue;
                    }

                    TimeSpan ts = (cs.endTime.Value > endtime ? cs.endTime.Value : endtime) - (cs.startTime.Value < starttime ? cs.startTime.Value : starttime);
                    TimeSpan ts1 = cs.endTime.Value - cs.startTime.Value;
                    TimeSpan ts2 = endtime - starttime;

                    if (ts < ts1 + ts2)
                    {
                        Response.Write("<script type='text/javascript'>alert('时间有冲突！');</script>");
                        return;
                    }

                }

                if (this.LabelEditingId.Text == "-1")//new shedule
                {

                    badminton.CourtSchedule cs = new badminton.CourtSchedule();
                    cs.courtId = Convert.ToInt32(this.DropDownList2.SelectedValue);
                    cs.startTime = starttime;
                    cs.endTime = endtime;
                    cs.scheduleTypeId = Convert.ToInt32(this.DropDownListUserType.SelectedValue);
                    cs.remark = this.TextBox1.Text;

                    bde.CourtSchedule.AddObject(cs);
                    bde.SaveChanges();

                    bindGridview();

                }
                else//insert edit
                {
                    int csid = Convert.ToInt32(this.LabelEditingId.Text);

                    badminton.CourtSchedule cs = bde.CourtSchedule.FirstOrDefault(p => p.id == csid);
                    if (cs!=null)
                    {
                        cs.startTime = starttime;
                        cs.endTime = endtime;
                        cs.scheduleTypeId = Convert.ToInt32(this.DropDownListUserType.SelectedValue);
                        cs.remark = this.TextBox1.Text;
                        bde.SaveChanges();

                        bindGridview();

                        //Response.Write("<script type='text/javascript'>alert('编辑成功！');</script>");
                    }

                }
            }
        }

        protected void OnHallChanged(object sender, EventArgs e)
        {
            int hallid = Convert.ToInt32(DropDownList1.SelectedValue);
            BadmintonEntities bde = new BadmintonEntities();
            Hall hall = bde.Hall.FirstOrDefault(p => p.id == hallid);
            if (hall == null)
            {
                return;
            }

            DropDownList2.Items.Clear();
            foreach (Court ct in hall.Court)
            {
                DropDownList2.Items.Add(new ListItem(ct.name, ct.id.ToString()));
            }

            bindGridview();

        }

        protected void OnCourtChanged(object sender, EventArgs e)
        {
            bindGridview();

        }
    }
}